package com.aaa.sso.util;

import com.aaa.common.bo.User;
import com.aaa.common.util.Result;
import com.aaa.sso.service.RemotUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;

public class UserRealm  extends AuthorizingRealm {

    @Resource
    private RemotUserService remoteUserService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 认证方法
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        //获取用户名
        String userName = (String)authenticationToken.getPrincipal();
        //调用远程方法 获取用户信息
        User user = remoteUserService.getUserByUsername(userName);
        //User user  = (User)result.getT();
        if(user==null){
            //用户不存在
            throw new AccountException();
        }
       /* if("2".equals(user.getStatus())){
            throw  new LockedAccountException();
        }*/
        return new SimpleAuthenticationInfo(user,user.getPassword(),
                ByteSource.Util.bytes(user.getSalt()),getName());
    }

//    @Override
//    public String getName() {
//        return "userRealm";
//    }
}
